package com.ddz.model;

import com.ddz.model.enums.PokerType;
import com.ddz.service.PokerService;
import org.json.JSONObject;

import javax.websocket.Session;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 房间类
 *
 * 管理玩家列表
 * 管理玩家手牌
 */
public class Room {
    private Map<Session, User> userMap = new ConcurrentHashMap<Session, User>();  // 用户信息
    private PokerService pokerService = new PokerService(); // 扑克牌管理类
    private JSONObject roominfo = new JSONObject(); // 房间状态：例如：等待中、进行中等
    private SimpleCounter current = new SimpleCounter(0, 2); // 计数器：目前玩家
    private SimpleCounter passnum = new SimpleCounter(0, 2, 2); // 计数器：跳过玩家数量

    public Room() {
        roominfo.put("PokerType", PokerType.NONE.toString());
        roominfo.put("Status", "waiting");  //waiting full started
        roominfo.put("lastpoker", "[]");
        roominfo.put("landlord", "");
    }

    public void adduser(Session session, User user) {
        userMap.put(session, user);
    }

    // 发牌
    public void dealPokers() {
        List<String> list = new ArrayList<>();
        for (User user : userMap.values()) {
            list.add(user.getUserId());
        }
        pokerService.dealPokers(list);
    }

    // 删除牌
    public void delPokers(String userId,List<Poker> pokers) {
        pokerService.delPokers(userId,pokers);
    }

    // 获取玩家手牌信息
    public List<String> userPokers(String userId) {
        return pokerService.getAllPokers(userId);
    }

    // 为玩家添加地主牌
    public void addloadPokers(String userid) {
        pokerService.addloadPokers(userid);
        // 记录地主玩家id
        roominfo.put("landlord", userid);
    }

    // 判断该玩家是否能出牌
    public boolean judge_current(String userId) {
        for (User user : userMap.values()) {
            if (user.getUserId().equals(userId)) {
                if (user.getOrder() == current.getCurrentValue()) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }

    // 返回当前出牌玩家ID
    public String current_user() {
        for (User user : userMap.values()) {
            if (user.getOrder() == current.getCurrentValue()) {
                return user.getUserId();
            }
        }
        return null;
    }

    // 设置下一位玩家出牌
    public void next_user() {
        current.increment();
    }

    // 设置当前出牌玩家
    public void setcurrent_user(String userid) {
        for (User user : userMap.values()) {
            if (user.getUserId().equals(userid)) {
                current.setCurrentValue(user.getOrder());
            }
        }
    }

    // 增加跳过数量
    public void passadd() {
        passnum.increment();
    }

    // 获取跳过数量
    public int getpassnum() {
        return passnum.getCurrentValue();
    }

    // 归零跳过数量
    public void clear_passnum(){
        passnum.setCurrentValue(0);
    }

    // 获取房间内所有玩家对象的列表
    public List<User> getAllPlayersInRoom() {
        if (userMap != null) {
            return new ArrayList<>(userMap.values());  // 返回房间内所有玩家对象的列表
        }
        return new ArrayList<>();  // 如果没有玩家，则返回空列表
    }

    public Map<Session, User> getUserMap() {
        return userMap;
    }

    // 获取房间的玩家数量
    public int getRoomPlayerCount() {
        return userMap != null ? userMap.size() : 0;
    }

    public String getRoominfo(String key) {
        return roominfo.getString(key);
    }

    public void setRoominfo(String key, String value) {
        roominfo.put(key, value);
    }

    @Override
    public String toString() {
        return "Room";
    }
}
